package com.sl.au.repository;


import com.sl.au.entity.WholeSetPrice;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

@Transactional(readOnly = true)
public interface WholeSetPriceRepository extends JpaRepository<WholeSetPrice, String> {
	/*
	 * 得到成套设备价格记录,分部门显示
	 */
    @Query(" select w from WholeSetPrice w where w.superior =?1 and "
    		+ "(w.wholeSetDevice.deviceName like '%'+?2+'%' or w.wholeSetDevice.type like '%'+?2+'%')")
    Page<WholeSetPrice> getWholeSet(String superior, String key, Pageable pageable);
    
    /**
     * 得到数据条数
     */
    @Query(" select count(r) from WholeSetPrice r where r.superior =?1 and "
    		+ "(r.wholeSetDevice.deviceName like '%'+?2+'%' or r.wholeSetDevice.type like '%'+?2+'%')")
    long getCount(String superior, String key);
    
    /*
	 * 得到成套设备价格记录,显示所有
	 */
    @Query(" select w from WholeSetPrice w where w.wholeSetDevice.deviceName like '%'+?1+'%' or w.wholeSetDevice.type like '%'+?1+'%'")
    Page<WholeSetPrice> getWholeSet(String key, Pageable pageable);
    
    @Query(" select count(r) from WholeSetPrice r where r.wholeSetDevice.deviceName like '%'+?1+'%' or r.wholeSetDevice.type like '%'+?1+'%'")
    long getCount(String key);
    /*
	 * 删除与成套ID相关的价格
	 */
	@Transactional
	@Modifying  
	@Query("delete from WholeSetPrice r where r.wholeSetDevice.id=?1 ")
	public void deleteWholeSetPrice(String wholeSetId);
	
	/*
	 * 更新成套价格记录中设备equipmentType根据成套ID
	 */
	@Transactional
	@Modifying  
	@Query("update WholeSetPrice r set r.equipmentType=?1 where r.wholeSetDevice.id=?2 ")
	public void updateTypeBywSetId(String type,String wholeSetId);
	
	/*
	 * 通过部门和成套设备型号找到价格
	 */
	@Query("select r from WholeSetPrice r where r.wholeSetDevice.type=?1 and r.superior=?2")
	WholeSetPrice findByTypeAndSuperior(String deviceType,String superior); 
}
